using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class ControlType : BaseItem
    {
        private ControlType() : base() { }
        private ControlType(Hashtable table) : base(table) { }
        private ControlType(Hashtable table, List<ControlType> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new ControlTypeDal();
        }

        public static ControlType CreateInstance()
        {
            return new ControlType();
        }

        private static ControlType GetItem(Hashtable table)
        {
            if (table != null)
                return new ControlType(table);
            else
                return null;
        }

        private static List<ControlType> GetItems(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<ControlType> tmp = new List<ControlType>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new ControlType(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static ControlType GetById(int id)
        {
            ControlTypeDal dal = new ControlTypeDal();
            return GetItem(dal.GetById(id));
        }

        public static List<ControlType> GetAllBySiteId(int siteId)
        {
            ControlTypeDal dal = new ControlTypeDal();
            return GetItems(dal.GetAllBySiteId(siteId));
        }

        protected override void Load(Hashtable table)
        {
            Id = (int)table[ControlTypeDal.Fields.Id];
            SiteId = (int?)table[ControlTypeDal.Fields.SiteId];
            Name = (string)table[ControlTypeDal.Fields.Name];
            AdminControlType = (string)table[ControlTypeDal.Fields.AdminControlType];
            AdminDisplayControlType = (string)table[ControlTypeDal.Fields.AdminDisplayControlType];
            AdminEditControlType = (string)table[ControlTypeDal.Fields.AdminEditControlType];
            FrontEndControlType = (string)table[ControlTypeDal.Fields.FrontEndControlType];
            Custom = (bool)table[ControlTypeDal.Fields.Custom];
            Notes = (string)table[ControlTypeDal.Fields.Notes];
        }

        public int Id { get; private set; }

        public int? SiteId { get; set; }

        public string Name { get; set; }

        public string AdminControlType { get; set; }

        public string AdminDisplayControlType { get; set; }

        public string AdminEditControlType { get; set; }
        
        public string FrontEndControlType { get; set; }

        public bool Custom { get; set; }

        public string Notes { get; set; }

        public new void Update()
        {
            base.Update();
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int refId = Id;
            ((ControlTypeDal)dal).Update(ref refId, SiteId, Name, AdminControlType, AdminDisplayControlType, AdminEditControlType, FrontEndControlType, Custom, Notes, ref concurencyId);
            Id = refId;
            return Id;
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((ControlTypeDal)dal).Delete(Id, concurencyId);
        }
    }
}
